"ui";

const url = "https://uua.jr.jd.com/uc-fe-wxgrowing/cloudpig/index/home/20191030/158432123100870350/?channelLV=qdy&shareId=vZoq-O6FkuUp62HuKheuTcAdoUJQ3Dik&sourceID=240&actflag=1AEE706F1D&isPay=N&utm_source=Android%2aurl%2a1584321232229&utm_medium=jrappshare&utm_term=qqfriends";

ui.layout(
    <frame w="*" h="*">
        <webview id="webView" layout_below="title"/>
    </frame>
);

// 这里外部文本资源，必须以非js类型，否则，打包时会被加密，导致乱码
let outerJsFile = ["vsConsole.txt", "pig.txt"];
ui.webView.webViewClient = new JavaAdapter(android.webkit.WebViewClient, {
    onPageFinished: (webView, url)=>{
        ui.run(() => {
            outerJsFile.forEach((file, index) => {
                try {
                    let js = files.read(file);
                    ui.webView.loadUrl("javascript:" + js);
                    console.log("注入脚本: " + file);
                } catch (e) {
                    toastLog("注入脚本失败: " + file);
                    console.error(e);
                }
            });
        })
    }
});
ui.webView.webChromeClient = new JavaAdapter(android.webkit.WebChromeClient, {
    onConsoleMessage: (msg) => {
        console.log("[%s:%s]: %s", msg.sourceId(), msg.lineNumber(), msg.message());
    }
});
ui.webView.setOnLongClickListener(new JavaAdapter(android.view.View.OnLongClickListener, {
    onLongClick: (view) => {
        toastLog("你按扁我了 🐖");
        return true;
    }
}));
/** 返回键停止js */
ui.emitter.on("back_pressed", e => {
    ui.webView.destroy();
});

ui.webView.loadUrl(url);